package com.example.tuike.common.utils;

import org.apache.ibatis.annotations.Param;

import java.util.Map;

/*** @Author supersaiya
 * @Description TODO

 * @Date 2025/2/11 20:55

 * @Version 1.0*/
public class ContentsSqlProvider {
    public String selectContentsByUserIdAndFeedId(@Param("feedId") final Integer feedId,
                                                  @Param("pageNumber") final int pageNumber) {
        // 每页显示的记录数
        int pageSize = 20;
        // 计算偏移量
        int offset = (pageNumber - 1) * pageSize;

        return new StringBuilder()
                .append("SELECT * FROM contents ")
                .append("WHERE feedId = #{feedId} ")
                .append("ORDER BY contentId DESC ")
                .append("LIMIT #{offset}, #{pageSize}")
                .toString();
    }
    public String selectContentsByFeedIdWithCursor(Map<String, Object> params) {
        Integer feedId = (Integer) params.get("feedId");
        Long cursor = (Long) params.get("cursor");
        Integer limit = (Integer) params.get("limit");

        StringBuilder sql = new StringBuilder();
        sql.append("SELECT * FROM contents WHERE feedId = #{feedId}");

        if (cursor != null) {
            // 如果 cursor 不为 null，表示从 cursor 指定的 contentId 之后的记录开始
            sql.append(" AND contentId < #{cursor}");
        }

        // 按 content_id 降序排列
        sql.append(" ORDER BY contentId DESC");

        // 限制返回的记录数
        sql.append(" LIMIT #{limit}");

        return sql.toString();
    }
}